package com.wxb.hello.auth.service;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Iterator;

/**
 * @author Feng Yu
 * @version V1.0
 * @date 2017-06-12
 */
public class CustUserDetails implements UserDetails{
    private static final long serialVersionUID = 1L;

    public static final String SESSION_ATTR_NAME = "com.cincc.cinom.web.auth.CustUserDetails";

    private String username;
    private String password;
    private String roles;

    private Object userInfo;

    private Boolean enabled = true;
    private Boolean accountNonExpired = true;
    private Boolean accountNonLocked = true;
    private Boolean credentialsNonExpired = true;


    public CustUserDetails(String username, String password, String roles, Object userInfo){
        if(userInfo != null){
            this.username = username;
            this.password = password;
            this.userInfo = userInfo;
            this.roles = roles;
            this.enabled = true;
            this.accountNonExpired = true;
            this.accountNonLocked = true;
            this.credentialsNonExpired = true;

        }
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return AuthorityUtils.commaSeparatedStringToAuthorityList(roles);
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getRoles() {
        return roles;
    }

    public void setRoles(String roles) {
        this.roles = roles;
    }

    public Object getUserInfo() {
        return userInfo;
    }

    public void setUserInfo(Object userInfo) {
        this.userInfo = userInfo;
    }

    public Boolean getEnabled() {
        return enabled;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    public void setAccountNonExpired(Boolean accountNonExpired) {
        this.accountNonExpired = accountNonExpired;
    }

    public void setAccountNonLocked(Boolean accountNonLocked) {
        this.accountNonLocked = accountNonLocked;
    }

    public void setCredentialsNonExpired(Boolean credentialsNonExpired) {
        this.credentialsNonExpired = credentialsNonExpired;
    }

    /**
     * 是否匹配权限
     */
    public boolean isMatchRole(String role){
        Iterator<? extends GrantedAuthority> its = this.getAuthorities().iterator();
        while (its.hasNext()){
            if(role.equals(its.next().getAuthority())){
                return true;
            }
        }
        return false;
    }

    /**
     * 是否过期
     */
    @Override
    public boolean isAccountNonExpired() {
        return this.accountNonExpired;
    }
    /**
     * 是否锁定
     */
    @Override
    public boolean isAccountNonLocked() {
        return this.accountNonLocked;
    }
    /**
     * 是否证书过期
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return this.credentialsNonExpired;
    }
    /**
     * 是否可用
     */
    @Override
    public boolean isEnabled() {
        return this.enabled;
    }
}
